﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using DataContracts;

namespace TrainingWorkshop.MVPWebsite.Role.Views
{
    public class SearchRolePresenter : Presenter<ISearchRoleView>
    {

        // NOTE: Uncomment the following code if you want ObjectBuilder to inject the module controller
        //       The code will not work in the Shell module, as a module controller is not created by default

        private IRoleController _roleController;

        public SearchRolePresenter([CreateNew] IRoleController roleController)
        {
            _roleController = roleController;
        }

        /// <summary>
        /// get all status from DB
        /// </summary>
        public void GetAllRoleStatus()
        {
            List<StatusDTO> allStatus = _roleController.GetAllRoleStatus();
            allStatus.Insert(0, new StatusDTO() { StatusId = 0, StatusName = "Please Select" });
            View.StatusList = allStatus;
        }

        /// <summary>
        /// get all authorities from DB
        /// </summary>
        public void GetAllAuthority()
        {
            View.AuthorityList = _roleController.GetAllAuthority();
        }

        /// <summary>
        /// get all role information from DB
        /// </summary>
        public void SearchRole()
        {
            int pageCnt;
            string status = "";
            if (View.Status != "Please Select")
            {
                status = View.Status;
            }


            View.RoleViewList = _roleController.SearchRole(View.RoleName, status, View.Authorities, View.SortExpression, View.SortDirection, View.PageSize, View.PageIndex, out pageCnt);

            List<RoleViewDTO> roleList = new List<RoleViewDTO>();
            roleList = View.RoleViewList;
            View.PageCount = pageCnt;
        }

        /// <summary>
        /// get all role information from DB
        /// </summary>
        public void GridBind()
        {
            View.GridBind();
            View.ShowPageInfo();
        }

        /// <summary>
        /// Judge the next sorting parameter
        /// </summary>
        public void JudgeSortParameter()
        {
            if (View.SortExpression == View.BindGridView.Attributes["SortExpression"])
            {
                //Get the next sorting state
                string a = View.BindGridView.Attributes["SortDirection"].ToString();

                if (a == "ASC")
                {
                    View.SortDirection = "DESC";
                }
                else
                {
                    View.SortDirection = "ASC";
                }
            }
            else
            {
                View.SortDirection = "ASC";
            }
        }

        /// <summary>
        /// add arrow to gridview column
        /// </summary>
        //public void ShowArrow()
        //{
        //    string[] gridViewHeaderName = { "RoleId", "RoleName", "AuthorityName", "LevelName" };

        //    for (int i = 0; i < 4; i++)
        //    {
        //        if (View.SortExpression == gridViewHeaderName[i])
        //        {
        //            if (View.SortDirection == "ASC")

        //                View.BindGridView.Columns[i].HeaderText = gridViewHeaderName[i] + "<font face=webdings>5</font>";

        //            else
        //                View.BindGridView.Columns[i].HeaderText = gridViewHeaderName[i] + "<font face=webdings>6</font>";
        //        }
        //        else
        //            View.BindGridView.Columns[i].HeaderText = gridViewHeaderName[i];
        //    }
        //}

        ///// <summary>
        ///// Show search result page information
        ///// </summary>
        //public void ShowPageInfo()
        //{
        //    View.lableCount.Text = View.PageCount.ToString();
        //    View.lableCurrentInfo.Text = View.PageIndex.ToString();
        //}

        /// <summary>
        /// implement paging 
        /// </summary>
        public void PageIndexButton_Click()
        {
            if (View.LinkButtonId == "lbtnFirst")
            {
                View.PageIndex = 1;
                GridBind();
            }
            else if (View.LinkButtonId == "lbtnPrev")
            {
                if (View.PageIndex > 1)
                {
                    View.PageIndex = View.PageIndex - 1;
                    GridBind();

                }
            }
            else if (View.LinkButtonId == "lbtnNext")
            {
                if (View.PageIndex < View.PageCount)
                {
                    View.PageIndex = View.PageIndex + 1;
                    GridBind();
                }
            }
            else
            {
                View.PageIndex = View.PageCount;
                GridBind();
            }
        }

        /// <summary>
        /// judge whether GridView control is visible
        /// </summary>
        /// <param name="falseOrTrue"></param>
        public void ShowPageFooter(bool falseOrTrue)
        {
            View.linkbutonFirst.Visible = falseOrTrue;
            View.linkbutonLast.Visible = falseOrTrue;
            View.linkbutonPrev.Visible = falseOrTrue;
            View.linkbutonNext.Visible = falseOrTrue;
            View.lableCount.Visible = falseOrTrue;
            View.lableCurrentInfo.Visible = falseOrTrue;
            View.lableSlash.Visible = falseOrTrue;
        }

        /// <summary>
        /// Set paging lable visible property
        /// </summary>
        public void CallPageFooterMethod()
        {
            if (View.PageCount > 1)
            {
                ShowPageFooter(true);
            }
            else
            {
                ShowPageFooter(false);
            }
        }

        /// <summary>
        /// Show the error message
        /// </summary>
        public void ShowMessage()
        {
            View.ErrorMessage = "Sorry!Do not have matching result.";
        }

        public override void OnViewLoaded()
        {
            // TODO: Implement code that will be executed every time the view loads

        }

        public override void OnViewInitialized()
        {
            // TODO: Implement code that will be executed the first time the view loads
            GetAllRoleStatus();
            GetAllAuthority();
            GridBind();
        }

        // TODO: Handle other view events and set state in the view
    }
}




